package com.fsc.comadapoc1.presentation.panels
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;

	import flash.events.EventDispatcher;
	
	import mx.collections.IList;

	[Event(name="loadPortfolio", type="events.PortfolioSearchEvent")]
	[ManagedEvents(names="loadPortfolio")]
	public class PendingTradePanelPM extends EventDispatcher
	{
		[Inject]
		[Bindable]
		public var model:PortfolioModel;
		
		[Bindable]
		public var page:Number = 1;
		
		/** The landing state **/
		public const LANDING_STATE : String = "landingState";
		
		/** The searching state. **/
		public const SEARCHING_STATE : String = "searchingState";
		
		/** The results state. **/
		public const RESULTS_STATE : String = "resultsState";
		
		/** The no results state. **/
		public const NO_RESULTS_STATE : String = "noResultsState";
		
		private var _currentState:String = LANDING_STATE;
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		

	
		
	
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
		
		
		[MessageHandler(selector="portfolioLoadComplete")]
		public function onSearchResults(event:LoadPortfolioEvent):void{
			trace("SearchPanelPM: Received ");
			if(model.currentPortfolio.pendingTrades &&	model.currentPortfolio.pendingTrades.length > 0){
				currentState = RESULTS_STATE;
				trace("lots of pending trade results")
			}else{
				currentState = NO_RESULTS_STATE;
				trace("no results pending trade")
			}
			
			//var v:Vector.<Object> = new Vector.<Object>();
			//for each (var i:Object in model.currentPortfolio.pendingTrades )
			//{
			//	v.push(i);
			//}
			//pagedList.storeItemsAt(v, page as int);
			
		}
		
		public function previousPage():void{
			page--;
			model.currentPortfolio.pendingTradesPage = page;
			dispatchEvent(new LoadPortfolioEvent(LoadPortfolioEvent.LOAD_PORTFOLIO,model.currentPortfolio));
		}
		
		public function nextPage():void{
			page++;
			model.currentPortfolio.pendingTradesPage = page;
			dispatchEvent(new LoadPortfolioEvent(LoadPortfolioEvent.LOAD_PORTFOLIO,model.currentPortfolio));
		}
		
		[MessageHandler(selector="portfolioLoadComplete")]
		public function onSearchResultsDoPageList(event:LoadPortfolioEvent):void{
	
		}
		
		public function checkThePrevious():Boolean{
			return (model.currentPortfolio.pendingTrades.length < 10?false:true);
		}
	}
}